package com.ruoyi.app.controller.product.service.impl;

import com.ruoyi.app.controller.product.mapper.IProductMapper;
import com.ruoyi.app.controller.product.service.IProductService;
import com.ruoyi.common.entity.DO.product.ShopProductAttributeDO;
import com.ruoyi.common.entity.VO.product.CreditProductVO;
import com.ruoyi.common.entity.VO.product.PetrolProductVO;
import com.ruoyi.common.entity.VO.product.RechargeProductVO;
import com.ruoyi.common.entity.VO.shop.ShopProductSearchVO;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Resource
    private IProductMapper productMapper;

    @Override
    public List<RechargeProductVO> searchRechargeWithBenefitList(ShopProductSearchVO searchVO) {
        List<RechargeProductVO> rechargeBenefitList = productMapper.searchRechargeBenefitList(searchVO);
        for (RechargeProductVO one:rechargeBenefitList
             ) {
            if (1== one.getIsBenefitEnable() && 1 == one.getIsAmount() && one.getBenefitAmount()>0){
                one.setShopRemark("赠送"+one.getBenefitAmount()+"元");
            }
        }
        return rechargeBenefitList;
    }

    @Override
    public List<CreditProductVO> searchCreditList(ShopProductSearchVO searchVO) {
        List<CreditProductVO> productList = productMapper.searchCreditList(searchVO);
        productList.forEach(map->map.setPrice(map.getPrice().setScale(0, BigDecimal.ROUND_DOWN)));
        return productList;
    }

    @Override
    public CreditProductVO getCreditDetail(ShopProductSearchVO searchVO) {
        CreditProductVO productDetail = productMapper.getCreditProductAllDetail(searchVO);
        if(null == productDetail){
            throw new BusinessException("商品已下架");
        }
        productDetail.setPrice(productDetail.getPrice().setScale(0,BigDecimal.ROUND_DOWN));
        return productDetail;
    }

    @Override
    public List<PetrolProductVO> searchPetrolList(ShopProductSearchVO searchVO) {
        List<PetrolProductVO> productList = productMapper.searchPetrolList(searchVO);
        return productList;
    }

    @Override
    public List<ShopProductAttributeDO> searchAttributeList(ShopProductSearchVO searchVO) {
        List<ShopProductAttributeDO> productAttrList = productMapper.searchAttributeList(searchVO);
        return productAttrList;
    }

//    @Override
//    @Transactional
//    public void handleBuy(ShopOrderDetailVO shopOrderDetailVO) {
//        int resultRow;
//        BigDecimal buyPrice;
//        ProductDO productInfo = productMapper.selectByPrimaryKey(shopOrderDetailVO.getProductId());
//        shopOrderDetailVO.setIsPromotion(productInfo.getIsPromotion());
//        if (productInfo == null) {
//            throw new BusinessException("下单失败，商品无效！");
//        }
//        if (productInfo.getStock() < shopOrderDetailVO.getProductQuantity()) {
//            throw new BusinessException("下单失败，库存不足！");
//        }
//        buyPrice = productInfo.getBuyPrice();
//        shopOrderDetailVO.setProductSn(productInfo.getProductSn());
//        resultRow = productMapper.reduceStock(shopOrderDetailVO.getProductId(), shopOrderDetailVO.getProductQuantity());
//        if (resultRow != 1) {
//            throw new BusinessException("下单失败，锁定库存失败！");
//        }
//        shopOrderDetailVO.setProductBuyPrice(buyPrice);
//        shopOrderDetailVO.setTotalBuyPrice(buyPrice.multiply(new BigDecimal(shopOrderDetailVO.getProductQuantity())));
//    }
}
